<template>
	<view class="mine-info flex-ct-y">
		<img src="@/static/newImg/log1 (1).png" class="imgs" />

		<button @click="login" class="FotBtn">授权登陆</button>
	</view>
</template>

<script>
	import cache from "@/utils/cache.js";
	export default {
		data() {
			return {
				userInfo: {
					avatar: '',
					name: ''
				}
			};
		},
		methods: {
			chooseavatar(e) {
				let avatarUrl = 'data:image/jpeg;base64,' + uni.getFileSystemManager().readFileSync(e.detail.avatarUrl,
					'base64')
				uni.setStorageSync('avatarUrl', avatarUrl);
				this.userInfo.avatar = avatarUrl
				let data = {
					name: this.userInfo.name,
					avatar: avatarUrl
				}
			},
			nameInput(e) {
				let nickName = e.detail.value
				wx.setStorageSync('nickName', nickName);
				this.userInfo.name = nickName
				let data = {
					name: e.detail.value,
					avatar: this.userInfo.avatar
				}
			},
			login() {
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						console.log(loginRes);
						// 登录成功，获取用户code
						// 发送code到后台换取openId, sessionKey, unionId
						uni.request({
							url: 'https://goods.farmhelper.top/wechat/token/getToken', // 你的登录API地址
							method: 'POST',
							data: {
								code: loginRes.code
							},
							success: (res) => {
								console.log(res, '登录成功111111111111');
								if (res.data.code == 1) {
									// 将用户信息和session存储到本地
									// uni.setStorageSync('token', res.data.result.token)
									uni.setStorageSync('userInfo', res.data.result.user);
									this.$store.state.hasLogin = true
									cache('user_token', res.data.result.token)
									uni.reLaunch({
										url: '/pages/tabBar/my/my'
									});
								} else {
									uni.showToast({
										title: '登录失败',
										icon: 'error'
									});
								}
							},
							fail: () => {
								uni.showToast({
									title: '请求失败',
									icon: 'none'
								});
							}
						});
					},
					fail: (err) => {
						console.log('uni.login 接口调用失败，将无法正常使用开放接口等服务', err);
						uni.showToast({
							title: '登录失败',
							icon: 'none'
						});
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.mine-info {
		width: 100%;
		height: 100%;

		.head-back {
			display: flex;
			align-items: center;
			margin-top: 15%;

			.title {
				flex: 1;
				font-weight: 500;
				font-size: 14px;
				color: #232323;
				text-align: center;
				margin-left: 10px;
			}
		}

		.imgs {
			width: 122px;
			height: 111px;
			display: block;
			text-align: center;
			margin: 0 auto;
			margin-top: 50px;
		}

		.FotBtn {
			margin-top: 25%;
			margin-left: 20px;
			margin-right: 20px;
			color: #fff;
			border-radius: 14px;
			background: linear-gradient(180deg, #637BE9 0%, #637BE9 100%);
		}
	}
</style>